<template>
  <div class="home">
    <!-- 顶部搜索区域 -->
    <div class="search-section">
      <div class="container">
        <div class="search-container">
          <div class="search-box">
            <input 
              v-model="searchKeyword" 
              type="text" 
              placeholder="搜索品牌或车型..."
              @keyup.enter="handleSearch"
              class="search-input"
            >
            <button @click="handleSearch" class="search-btn">搜索</button>
          </div>
        </div>
      </div>
    </div>

    <!-- 热门品牌展示 -->
    <div class="brands-section">
      <div class="container">
        <h2 class="section-title">热门汽车品牌</h2>
        <p class="section-subtitle">精选全球知名汽车品牌，为您提供多样化选择</p>
        
        <div v-if="loading" class="loading-container">
          <div class="loading-spinner"></div>
          <p>加载中...</p>
        </div>
        
        <div v-else class="brands-grid">
          <div 
            v-for="brand in popularBrands" 
            :key="brand.brand_id"
            class="brand-card"
            @click="showBrandCars(brand)"
          >
            <div class="brand-logo">
              <div class="logo-placeholder">{{ getBrandInitial(brand.brand_name) }}</div>
            </div>
            <div class="brand-info">
              <h3 class="brand-name">{{ brand.brand_name }}</h3>
              <p class="brand-intro">{{ brand.brand_intro || '暂无详细介绍' }}</p>
              <div class="brand-stats">
                <span class="car-count">{{ brand.car_count || 0 }} 款车型</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 车型展示模态框 -->
    <div v-if="showCarsModal" class="modal-overlay" @click="closeModal">
      <div class="modal-content" @click.stop>
        <div class="modal-header">
          <h3>{{ selectedBrand?.brand_name }} - 热门车型</h3>
          <button @click="closeModal" class="close-btn">×</button>
        </div>
        <div class="cars-list">
          <div 
            v-for="car in brandCars" 
            :key="car.id"
            class="car-item"
          >
            <div class="car-header">
              <h4 class="car-model">{{ car.model_name }}</h4>
              <span class="car-price" v-if="car.model_price">¥{{ car.model_price }}万</span>
            </div>
            <div class="car-details">
              <span class="car-series">{{ car.series_name }}</span>
              <span class="car-energy">{{ car.energy_type }}</span>
              <span class="car-level">{{ car.level }}</span>
              <span class="car-launch-date" v-if="car.launch_date">{{ car.launch_date }}上市</span>
            </div>
          </div>
        </div>
        <div v-if="brandCars.length === 0" class="no-cars">
          <p>暂无车型数据</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { carApiService } from '../api/car.js'

export default {
  name: 'Home',
  data() {
    return {
      popularBrands: [],
      loading: false,
      searchKeyword: '',
      showCarsModal: false,
      selectedBrand: null,
      brandCars: []
    }
  },
  async mounted() {
    await this.loadPopularBrands()
  },
  methods: {
    async loadPopularBrands() {
      this.loading = true
      try {
        const response = await carApiService.getPopularBrands(10)
        if (response.success) {
          this.popularBrands = response.data
        } else {
          console.error('获取品牌数据失败:', response.message)
          // 使用模拟数据作为备选
          this.popularBrands = this.getMockBrands()
        }
      } catch (error) {
        console.error('API请求失败:', error)
        // 使用模拟数据作为备选
        this.popularBrands = this.getMockBrands()
      } finally {
        this.loading = false
      }
    },
    
    async showBrandCars(brand) {
      this.selectedBrand = brand
      this.showCarsModal = true
      
      try {
        const response = await carApiService.getPopularCarsByBrand(brand.brand_id, 3)
        if (response.success) {
          this.brandCars = response.data
        } else {
          console.error('获取车型数据失败:', response.message)
          this.brandCars = []
        }
      } catch (error) {
        console.error('获取车型数据失败:', error)
        this.brandCars = []
      }
    },
    
    closeModal() {
      this.showCarsModal = false
      this.selectedBrand = null
      this.brandCars = []
    },
    
    handleSearch() {
      if (this.searchKeyword.trim()) {
        // 这里可以添加搜索功能
        console.log('搜索关键词:', this.searchKeyword)
        // 实际项目中可以跳转到搜索页面或显示搜索结果
      }
    },
    
    getBrandInitial(name) {
      return name ? name.charAt(0) : '车'
    },
    
    getCarInitial(name) {
      return name ? name.substring(0, 2) : '车型'
    },
    
    getMockBrands() {
      // 模拟品牌数据，用于API不可用时展示
      return [
        { brand_id: 1, brand_name: '比亚迪', brand_intro: '中国新能源汽车领导者', car_count: 15 },
        { brand_id: 2, brand_name: '鸿蒙智行', brand_intro: '华为与赛力斯联合打造', car_count: 8 },
        { brand_id: 3, brand_name: '特斯拉', brand_intro: '全球电动汽车领导者', car_count: 6 },
        { brand_id: 4, brand_name: '吉利', brand_intro: '全球化汽车品牌', car_count: 10 },
        { brand_id: 5, brand_name: '长安', brand_intro: '中国自主品牌领军企业', car_count: 12 },
        { brand_id: 6, brand_name: '奇瑞', brand_intro: '中国自主汽车品牌', car_count: 9 },
        { brand_id: 7, brand_name: '奔驰', brand_intro: '德国豪华汽车品牌', car_count: 20 },
        { brand_id: 8, brand_name: '宝马', brand_intro: '德国豪华汽车制造商', car_count: 18 },
        { brand_id: 9, brand_name: '奥迪', brand_intro: '德国豪华汽车品牌', car_count: 16 }
      ]
    }
  }
}
</script>

<style scoped>
.home {
  min-height: 100vh;
  background: white;
}

/* 顶部搜索区域样式 */
.search-section {
  padding: 4rem 2rem;
  position: relative;
  overflow: hidden;
}

.search-container {
  max-width: 700px;
  margin: 0 auto;
  position: relative;
  z-index: 1;
}

.search-box {
  display: flex;
  max-width: 100%;
  background: white;
  border-radius: 50px;
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(0,0,0,0.1);
  border: 1px solid #e0e0e0;
  transition: all 0.3s ease;
}

.search-box:focus-within {
  box-shadow: 0 8px 30px rgba(0,0,0,0.15);
  transform: translateY(-2px);
  border-color: #4CAF50;
}

.search-input {
  flex: 1;
  border: none;
  padding: 1.4rem 2rem;
  font-size: 1.1rem;
  outline: none;
  background: transparent;
  color: #333;
  font-weight: 400;
}

.search-input::placeholder {
  color: #999;
}

.search-btn {
  background: linear-gradient(135deg, #4CAF50, #66BB6A);
  color: white;
  border: none;
  padding: 1.4rem 3rem;
  cursor: pointer;
  font-weight: 600;
  transition: all 0.3s ease;
  border-left: 1px solid #e0e0e0;
  font-size: 1.1rem;
  white-space: nowrap;
}

.search-btn:hover {
  background: linear-gradient(135deg, #2E7D32, #4CAF50);
  transform: translateX(-2px);
}

/* 品牌区域样式 - 优化视觉层次 */
.brands-section {
  padding: 5rem 2rem;
  background: white;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
}

.section-title {
  text-align: center;
  font-size: 2.8rem;
  color: #2E7D32;
  margin-bottom: 1rem;
  font-weight: 700;
  position: relative;
}

.section-title::after {
  content: '';
  position: absolute;
  bottom: -10px;
  left: 50%;
  transform: translateX(-50%);
  width: 80px;
  height: 4px;
  background: linear-gradient(135deg, #4CAF50, #66BB6A);
  border-radius: 2px;
}

.section-subtitle {
  text-align: center;
  color: #666;
  font-size: 1.2rem;
  margin-bottom: 4rem;
  line-height: 1.6;
}

.loading-container {
  text-align: center;
  padding: 6rem;
}

.loading-spinner {
  width: 50px;
  height: 50px;
  border: 4px solid #f0f4f8;
  border-top: 4px solid #4CAF50;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin: 0 auto 1.5rem;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.brands-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 2.5rem;
  margin-top: 3rem;
}

.brand-card {
  background: white;
  border-radius: 20px;
  padding: 2.5rem;
  box-shadow: 0 8px 32px rgba(46, 125, 50, 0.1);
  cursor: pointer;
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  display: flex;
  align-items: center;
  gap: 2rem;
  border: 1px solid #f0f0f0;
  position: relative;
  overflow: hidden;
}

.brand-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(76, 175, 80, 0.05), transparent);
  transition: left 0.6s ease;
}

.brand-card:hover::before {
  left: 100%;
}

.brand-card:hover {
  transform: translateY(-8px);
  box-shadow: 0 20px 50px rgba(46, 125, 50, 0.15);
  border-color: #e0f2e0;
}

.brand-logo {
  flex-shrink: 0;
}

.logo-placeholder {
  width: 90px;
  height: 90px;
  background: linear-gradient(135deg, #2E7D32, #4CAF50);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 2.2rem;
  font-weight: bold;
  box-shadow: 0 4px 12px rgba(46, 125, 50, 0.3);
  transition: transform 0.3s ease;
}

.brand-card:hover .logo-placeholder {
  transform: scale(1.1);
}

.brand-info {
  flex: 1;
}

.brand-name {
  font-size: 1.5rem;
  font-weight: 600;
  color: #2E7D32;
  margin-bottom: 0.8rem;
}

.brand-intro {
  color: #666;
  font-size: 0.95rem;
  margin-bottom: 1.2rem;
  line-height: 1.5;
}

.brand-stats {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.car-count {
  background: linear-gradient(135deg, #e8f5e9, #c8e6c9);
  color: #2E7D32;
  padding: 0.5rem 1.2rem;
  border-radius: 25px;
  font-size: 0.9rem;
  font-weight: 600;
  border: 1px solid #c8e6c9;
}

/* 模态框样式 - 与主题色调统一 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 2rem;
  backdrop-filter: blur(5px);
}

.modal-content {
  background: white;
  border-radius: 20px;
  max-width: 900px;
  width: 100%;
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: 0 25px 80px rgba(0,0,0,0.3);
  border: 1px solid #f0f0f0;
}

.modal-header {
  padding: 2.5rem 2.5rem 1.5rem;
  border-bottom: 1px solid #f0f0f0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: linear-gradient(135deg, #f8f9fa, #f0f4f8);
  border-radius: 20px 20px 0 0;
}

.modal-header h3 {
  margin: 0;
  color: #2E7D32;
  font-size: 1.6rem;
  font-weight: 600;
}

.close-btn {
  background: none;
  border: none;
  font-size: 2.2rem;
  cursor: pointer;
  color: #999;
  line-height: 1;
  transition: color 0.3s ease;
  padding: 0.5rem;
  border-radius: 50%;
}

.close-btn:hover {
  color: #2E7D32;
  background: #f0f4f8;
}

.cars-list {
  padding: 2.5rem;
}

.car-item {
  background: linear-gradient(135deg, #f8f9fa, #f0f4f8);
  border-radius: 15px;
  padding: 2rem;
  margin-bottom: 1.5rem;
  transition: all 0.3s ease;
  border-left: 6px solid #4CAF50;
  border: 1px solid #e0e0e0;
}

.car-item:hover {
  transform: translateX(8px);
  box-shadow: 0 10px 30px rgba(46, 125, 50, 0.1);
  border-left-color: #2E7D32;
}

.car-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
}

.car-model {
  font-size: 1.3rem;
  font-weight: 600;
  color: #2E7D32;
  margin: 0;
}

.car-price {
  background: linear-gradient(135deg, #ffebee, #ffcdd2);
  color: #c62828;
  padding: 0.6rem 1.2rem;
  border-radius: 25px;
  font-size: 1rem;
  font-weight: 600;
  border: 1px solid #ffcdd2;
}

.car-details {
  display: flex;
  gap: 1rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}

.car-series, .car-energy, .car-level, .car-launch-date {
  background: linear-gradient(135deg, #e8f5e9, #c8e6c9);
  color: #2E7D32;
  padding: 0.4rem 1rem;
  border-radius: 20px;
  font-size: 0.9rem;
  font-weight: 500;
  margin: 0;
  border: 1px solid #c8e6c9;
}

.no-cars {
  text-align: center;
  padding: 4rem;
  color: #999;
  font-size: 1.1rem;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .search-section {
    padding: 3rem 1.5rem;
  }
  
  .search-container {
    max-width: 90%;
  }
  
  .search-input {
    padding: 1.2rem 1.5rem;
  }
  
  .search-btn {
    padding: 1.2rem 2rem;
  }
  
  .section-title {
    font-size: 2.2rem;
  }
  
  .brands-grid {
    grid-template-columns: 1fr;
    gap: 2rem;
  }
  
  .brand-card {
    flex-direction: column;
    text-align: center;
    padding: 2rem;
  }
  
  .modal-content {
    margin: 1rem;
    max-height: 90vh;
  }
  
  .modal-header {
    padding: 2rem 2rem 1rem;
  }
  
  .cars-list {
    padding: 2rem;
  }
}

@media (max-width: 480px) {
  .search-section {
    padding: 2rem 1rem;
  }
  
  .search-container {
    max-width: 100%;
  }
  
  .search-box {
    flex-direction: column;
    border-radius: 15px;
    max-width: 100%;
  }
  
  .search-input {
    border-radius: 15px 15px 0 0;
    padding: 1rem 1.5rem;
    text-align: center;
  }
  
  .search-btn {
    border-radius: 0 0 15px 15px;
    border-left: none;
    border-top: 1px solid rgba(255, 255, 255, 0.2);
    padding: 1rem 2rem;
    width: 100%;
  }
  
  .brands-section {
    padding: 3rem 1rem;
  }
  
  .brand-card {
    padding: 1.5rem;
  }
  
  .logo-placeholder {
    width: 70px;
    height: 70px;
    font-size: 1.8rem;
  }
  
  .car-details {
    gap: 0.8rem;
  }
  
  .car-series, .car-energy, .car-level, .car-launch-date {
    padding: 0.3rem 0.8rem;
    font-size: 0.85rem;
  }
}
</style>